package com.campus.interceptor;

import com.campus.util.JwtUtils;
import com.campus.web.vo.ResponseResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;


@Component
public class LoginInterceptor  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwt=request.getHeader("token");
        if(!StringUtils.hasLength(jwt)){
            ResponseResult.error(HttpStatus.UNAUTHORIZED,"请求头token为空，返回未登录信息");
            return false;
        }
        try {
            JwtUtils.verifyToken(jwt);
        }catch (Exception e){
            e.printStackTrace();
            ResponseResult.error(HttpStatus.UNAUTHORIZED,"解析令牌失败");
            return false;
        }
        return true;
    }
}
